package cn.kgc.controller;

import cn.kgc.base.vo.SecKillSessionAndSecKillSkuVO;

import cn.kgc.base.vo.SeckillSessionVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import cn.kgc.entity.SmsSeckillSession;
import cn.kgc.dto.SmsSeckillSessionAddDTO;
import cn.kgc.dto.SmsSeckillSessionUpdateDTO;
import cn.kgc.service.SmsSeckillSessionService;
import io.swagger.annotations.ApiImplicitParams;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import cn.kgc.commons.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.kgc.entity.SmsSeckillSession;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * (SmsSeckillSession)表控制层
 *
 * @author 石浩杰
 * @since 2023-01-07 09:39:21
 */
@RestController
@RequestMapping("/sms/smsSeckillSession")
@Api
@Validated
public class SmsSeckillSessionController {
    /**
     * 服务对象
     */
    @Resource
    private SmsSeckillSessionService smsSeckillSessionService;


    @GetMapping("/getSecSession")
    @ApiOperation(value = "提供现在正在秒杀的场次的信息",
            produces = "application/json;charset=UTF-8",httpMethod = "GET",response = Result.class)
    @CrossOrigin
    public List<SeckillSessionVO> getSecSessions(){
        Date date=new Date();
        LambdaQueryWrapper<SmsSeckillSession>lambdaQueryWrapper=
                new LambdaQueryWrapper<>();
        lambdaQueryWrapper.lt(SmsSeckillSession::getStartTime,date);
        lambdaQueryWrapper.ge(SmsSeckillSession::getEndTime,date);

        List<SmsSeckillSession> list =
                smsSeckillSessionService.list(lambdaQueryWrapper);

        List<SeckillSessionVO>result=list.stream().map(smsSeckillSession -> {
            SeckillSessionVO seckillSessionVO=new SeckillSessionVO();
            BeanUtils.copyProperties(smsSeckillSession,seckillSessionVO);
            return seckillSessionVO;
        }).collect(Collectors.toList());

        return result;
    }

    //提供当天需要秒杀的场次以及场次对应的商品信息
    @GetMapping("/getSecSessionAndSecSku")
    @ApiOperation(value = "提供当天需要秒杀的场次以及场次对应的商品信息", produces = "application/json;charset=UTF-8",httpMethod = "GET",response = Result.class)
    @CrossOrigin
    public List<SecKillSessionAndSecKillSkuVO> getSecSessionAndSecSku(){
        return smsSeckillSessionService.getSecSessionAndSecSku();
    }

    /**
     * 分页查询所有数据
     *
     * @param current 分页对象
     * @return 所有数据
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询所有数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "name",value = "名字",paramType = "query",dataType = "string")
    })
    @CrossOrigin
    public IPage<SmsSeckillSession> selectAll(@RequestParam(defaultValue = "1") int current,
                                              @RequestParam(defaultValue = "5") int pageSize,
                                              @RequestParam(name = "name")String name) {
        //todo 按照时间段查询  开始时间 结束时间
        IPage<SmsSeckillSession> page = new Page(current, pageSize);
        LambdaQueryWrapper<SmsSeckillSession> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotBlank(name),SmsSeckillSession::getName,name);
        return smsSeckillSessionService.page(page,lambdaQueryWrapper);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @CrossOrigin
    @ApiOperation(value = "通过主键查询单条数据", produces = "application/json;charset=utf-8",
            httpMethod = "GET", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    public SmsSeckillSession selectOne(@PathVariable Serializable id) {
        return this.smsSeckillSessionService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param smsSeckillSessionDTO 实体对象
     * @return 新增结果
     */
    @PostMapping("/insert")
    @ApiOperation(value = "新增场次及场次对应商品信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean insert(@RequestBody SmsSeckillSessionAddDTO smsSeckillSessionDTO) {
        //todo 场次的开始时间和场次的结束时间必须在同一天
        //todo 场次的结束时间大于场次的开始时间
        //todo 场次的开始时间大于1:00
        //todo
        return this.smsSeckillSessionService.saveSessionAndSeckillSku(smsSeckillSessionDTO);
    }

    //todo 将场次改为启用，场次本身是未启动，场次的开始时间要小于定时的时间(每日的凌晨1点)
    //将场次改为未启用条件，
    /**
     * 修改数据
     *
     * @param smsSeckillSessionDTO 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改信息", produces = "application/json;charset=utf-8",
            httpMethod = "POST", response = Result.class)
    @CrossOrigin
    public boolean update(@RequestBody SmsSeckillSessionUpdateDTO smsSeckillSessionDTO) {
        SmsSeckillSession smsSeckillSession = this.smsSeckillSessionService.getById(smsSeckillSessionDTO.getId());
        BeanUtils.copyProperties(smsSeckillSessionDTO, smsSeckillSession);
        return this.smsSeckillSessionService.updateById(smsSeckillSession);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "根据主键删除信息", produces = "application/json;charset=utf-8",
            httpMethod = "DELETE", response = Result.class)
    @ApiImplicitParam(name = "id", value = "主键", paramType = "path", dataType = "int")
    @CrossOrigin
    public boolean delete(@PathVariable Long id) {
        return this.smsSeckillSessionService.removeById(id);
    }
}
